<template>
	<a-row :gutter="10">
		<a-col :xs="24" :sm="24" :md="24" :lg="5" :xl="5">
			<a-card :bordered="false" :loading="cardLoading">
				<a-tree
					v-if="treeData.length > 0"
					v-model:expandedKeys="defaultExpandedKeys"
					:tree-data="treeData"
					:field-names="treeFieldNames"
					@select="treeSelect"
				>
				</a-tree>
				<a-empty v-else :image="Empty.PRESENTED_IMAGE_SIMPLE" />
			</a-card>
		</a-col>
		<a-col :xs="24" :sm="24" :md="24" :lg="19" :xl="19">
			<a-card :bordered="false">
				<a-form ref="searchFormRef" name="advanced_search" :model="searchFormState" class="ant-advanced-search-form">
					<a-row :gutter="24">
						<a-col :span="6">
							<a-form-item label="印章名称" name="title">
								<a-input v-model:value="searchFormState.title" placeholder="请输入印章名称" />
							</a-form-item>
						</a-col>
						<a-col :span="6">
							<a-button type="primary" @click="tableRef.refresh(true)">查询</a-button>
							<a-button style="margin: 0 8px" @click="reset">重置</a-button>
						</a-col>
					</a-row>
				</a-form>
				<s-table
					ref="tableRef"
					:columns="columns"
					:data="loadData"
					:alert="options.alert.show"
					bordered
					:row-key="(record) => record.id"
					:tool-config="toolConfig"
					:row-selection="options.rowSelection"
				>
					<template #operator class="table-operator">
						<a-space>
							<a-button type="primary" @click="formRef.onOpen()" v-if="hasPerm('flwSealAdd')">
								<template #icon><plus-outlined /></template>
								新增
							</a-button>
							<xn-batch-delete
								v-if="hasPerm('flwSealBatchDelete')"
								:selectedRowKeys="selectedRowKeys"
								@batchDelete="deleteBatchFlwSeal"
							/>
						</a-space>
					</template>
					<template #bodyCell="{ column, record }">
						<template v-if="column.dataIndex === 'type'">
							{{ $TOOL.dictTypeData('FLW_SEAL_TYPE', record.type) }}
						</template>
						<template v-if="column.dataIndex === 'isDefault'">
							<a-tag :bordered="false" v-if="record.isDefault === 'NO'">{{
								$TOOL.dictTypeData('COMMON_WHETHER', record.isDefault)
							}}</a-tag>
							<a-tag :bordered="false" color="processing" v-if="record.isDefault === 'YES'">{{
								$TOOL.dictTypeData('COMMON_WHETHER', record.isDefault)
							}}</a-tag>
						</template>
						<template v-if="column.dataIndex === 'sealImg'">
							<a-image :src="record.sealImg" style="width: 60px; height: 60px" />
						</template>
						<template v-if="column.dataIndex === 'action'">
							<a-space>
								<a @click="setAsDefault(record)" v-if="hasPerm('flwSealSetAsDefault') && record.isDefault === 'NO'"
									>设为默认</a
								>
								<a-divider
									type="vertical"
									v-if="hasPerm(['flwSealEdit', 'flwSealSetAsDefault'], 'and') && record.isDefault === 'NO'"
								/>
								<a @click="formRef.onOpen(record)" v-if="hasPerm('flwSealEdit')">编辑</a>
								<a-divider type="vertical" v-if="hasPerm(['flwSealEdit', 'flwSealDelete'], 'and')" />
								<a-popconfirm title="确定要删除吗？" @confirm="deleteFlwSeal(record)">
									<a-button type="link" danger size="small" v-if="hasPerm('flwSealDelete')">删除</a-button>
								</a-popconfirm>
							</a-space>
						</template>
					</template>
				</s-table>
			</a-card>
		</a-col>
		<Form ref="formRef" @successful="tableRef.refresh()" />
	</a-row>
</template>

<script setup name="seal">
	import { message } from 'ant-design-vue'
	import { cloneDeep } from 'lodash-es'
	import Form from './form.vue'
	import flwSealApi from '@/api/flw/flwSealApi'
	import { Empty } from 'ant-design-vue'
	import { isEmpty } from 'lodash-es'
	const searchFormState = ref({})
	const searchFormRef = ref()
	const tableRef = ref()
	const formRef = ref()
	const toolConfig = { refresh: true, height: true, columnSetting: true, striped: false }
	const treeData = ref([])
	const cardLoading = ref(false)
	const defaultExpandedKeys = ref([])
	const treeFieldNames = { children: 'children', title: 'name', key: 'id' }

	const columns = [
		{
			title: '印章名称',
			dataIndex: 'title'
		},
		{
			title: '印章类型',
			dataIndex: 'type'
		},
		{
			title: '印章图片',
			dataIndex: 'sealImg'
		},
		{
			title: '是否默认',
			dataIndex: 'isDefault'
		}
	]
	// 操作栏通过权限判断是否显示
	if (hasPerm(['flwSealEdit', 'flwSealDelete'])) {
		columns.push({
			title: '操作',
			dataIndex: 'action',
			align: 'center',
			width: '220px'
		})
	}
	const selectedRowKeys = ref([])
	// 列表选择配置
	const options = {
		// columns数字类型字段加入 needTotal: true 可以勾选自动算账
		alert: {
			show: false,
			clear: () => {
				selectedRowKeys.value = ref([])
			}
		},
		rowSelection: {
			onChange: (selectedRowKey, selectedRows) => {
				selectedRowKeys.value = selectedRowKey
			}
		}
	}
	// 加载左侧的树
	const loadTreeData = () => {
		flwSealApi
			.flwSealOrgTreeSelector()
			.then((res) => {
				cardLoading.value = false
				if (res !== null) {
					treeData.value = res
					if (isEmpty(defaultExpandedKeys.value)) {
						// 默认展开2级
						treeData.value.forEach((item) => {
							// 因为0的顶级
							if (item.parentId === '0') {
								defaultExpandedKeys.value.push(item.id)
								// 取到下级ID
								if (item.children) {
									item.children.forEach((items) => {
										defaultExpandedKeys.value.push(items.id)
									})
								}
							}
						})
					}
				}
			})
			.finally(() => {
				cardLoading.value = false
			})
	}
	const loadData = (parameter) => {
		loadTreeData()
		const searchFormParam = cloneDeep(searchFormState.value)
		return flwSealApi.flwSealPage(Object.assign(parameter, searchFormParam)).then((data) => {
			return data
		})
	}
	// 点击树查询
	const treeSelect = (selectedKeys) => {
		if (selectedKeys.length > 0) {
			searchFormState.value.orgId = selectedKeys.toString()
		} else {
			delete searchFormState.value.orgId
		}
		tableRef.value.refresh(true)
	}
	// 重置
	const reset = () => {
		searchFormRef.value.resetFields()
		tableRef.value.refresh(true)
	}
	// 设为默认
	const setAsDefault = (record) => {
		const param = {
			id: record.id
		}
		flwSealApi.flwSetAsDefault(param).then(() => {
			message.success('操作成功')
			tableRef.value.refresh()
		})
	}
	// 删除
	const deleteFlwSeal = (record) => {
		let params = [
			{
				id: record.id
			}
		]
		flwSealApi.flwSealDelete(params).then(() => {
			tableRef.value.refresh()
		})
	}
	// 批量删除
	const deleteBatchFlwSeal = (params) => {
		flwSealApi.flwSealDelete(params).then(() => {
			tableRef.value.clearRefreshSelected()
		})
	}
</script>
<style lang="less" scoped>
	.hand-sign {
		width: 120px !important;
		height: 80px !important;
	}
	.ele-sign {
		width: 80px;
		height: 80px;
	}
</style>
